﻿using BCCommon;
using BCDto.WeChatComponent.WeChatUserTag;
using BCService.WeChatComponent.WeChatUserTagService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace BCTMSRESTful.Controllers.Management.WeChat
{
    /// <summary>
    /// 微信用户标签控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class WeChatUserTagController : ManagementBaseController
    {
        private readonly IWeChatUserTagService weChatUserTagService;

        public WeChatUserTagController(IWeChatUserTagService weChatUserTagService)
        {
            this.weChatUserTagService = weChatUserTagService;
        }
        /// <summary>
        /// 添加微信用户标签。
        /// </summary>
        /// <param name="name">微信用户标签</param>
        /// <response code="201">已添加成功的微信用户标签。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(WeChatUserTagDto), 201)]
        public IActionResult Post([FromQuery(Name ="name")]string name)
        {
            WeChatUserTagDto result = this.weChatUserTagService.Add(name);
            return this.Created(string.Format("/api/v1/sim/WeChatUserTag/{0}", result.WeChatUserTagId), result);
        }
        /// <summary>
        /// 修改微信用户标签。
        /// </summary>
        /// <param name="id">标签Id</param>
        /// <param name="name">微信用户标签</param>
        /// <response code="200">修改成功的微信用户标签。</response>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ProducesResponseType(typeof(WeChatUserTagDto), 200)]
        public IActionResult Put([FromRoute(Name ="id")]int id,[FromQuery(Name = "name")]string name)
        {
        
            return this.Ok(this.weChatUserTagService.Update(id,name));
        }
        /// <summary>
        /// 获取微信用户标签信息列表(带分页)。
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">微信用户标签信息列表</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<WeChatUserTagDto>), 200)]
        public IActionResult Query([FromQuery(Name = "name")]string name,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.weChatUserTagService.Get(name,start,end,ascending,index,size));
        }

        /// <summary>
        /// 删除微信用户标签。
        /// </summary>
        /// <param name="id">标签id</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult Delete([FromRoute(Name = "id")]int id)
        {
            this.weChatUserTagService.Delete(id);
            return this.NoContent();
        }

        /// <summary>
        /// 同步微信永久素材到本地
        /// </summary>
        /// <response code="200"></response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult SynchronizeWeChatUserTags()
        {
            this.weChatUserTagService.SynchronizeUserTags();

            return this.Ok(true);
        }

        /// <summary>
        /// 获取微信用户标签信息列表。
        /// </summary>
        /// <response code="200">微信用户标签信息列表</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<WeChatUserTagDto>), 200)]
        public IActionResult Get()
        {
            return this.Ok(this.weChatUserTagService.Get());
        }
        /// <summary>
        /// 为微信用户打标签
        /// </summary>
        /// <response code="200">打标签是否成功</response>
        /// <returns></returns>
        [HttpPut("[action]/{openid}/{tagid}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult Mark([FromRoute(Name = "openid")]string openid, [FromRoute(Name = "tagid")]long tagid)
        {
            this.weChatUserTagService.Mark(tagid, openid);
            return this.Ok(true);
        }
        /// <summary>
        /// 移除微信用户标签
        /// </summary>
        /// <response code="200">移除标签是否成功</response>
        /// <returns></returns>
        [HttpPut("[action]/{openid}/{tagid}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult UnMark([FromRoute(Name = "openid")]string openid, [FromRoute(Name = "tagid")]long tagid)
        {
            this.weChatUserTagService.UnMark(tagid, openid);
            return this.Ok(true);
        }
    }
}
